import request from '@/utils/request'


export const sendChatMessage = async (data, token) => {
    const formData = new FormData();
    formData.append('prompt', data.prompt);
    formData.append('chatId', data.chatId);
    formData.append('model', data.model);

    if (data.files && data.files.length > 0) {
        data.files.forEach(file => {
            formData.append('files', file);
        });
    }

    // 使用fetch API实现流式读取
    const response = await fetch('/api/ai/chat', {
        method: 'POST',
        headers: {
            // 确保正确传递Authorization头
            'Authorization': token ? token : '',
        },
        body: formData
    })

    if (!response.ok) {
        console.error('请求失败:', response.statusText)
        throw new Error(`HTTP error! status: ${response.status}`)
    }

    return response.body.getReader()
}

/// 获取所有会话列表
export const getChatHistory = (type) => {
    return request.get(`/ai/history/${type}`);
}

// 获取指定会话的消息历史
export const getChatMessages = (type, chatId) => {
    return request.get(`/ai/history/${type}/${chatId}`);
}

// 删除会话
export const deleteChatHistory = (type, chatId) => {
    return request.delete(`/ai/history/${type}/${chatId}`);
};

// 删除所有会话
export const deleteAllChatHistories = (type) => {
    return request.delete(`/ai/history/all/${type}`);
};
